﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.TeamFoundation;
using Ltf.Remoting.TFAccess;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace Ltf.MonitorLizard.Controls
{
    public partial class VersionComboControl : ComboBox
    {
        private bool isLoading = false;

        public VersionComboControl()
        {
            InitializeComponent();
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            if (isLoading)
                return;

            base.OnSelectedIndexChanged(e);
        }

        protected override void OnSelectedItemChanged(EventArgs e)
        {
            if (isLoading)
                return;

            base.OnSelectedItemChanged(e);
        }

        protected override void OnSelectedValueChanged(EventArgs e)
        {
            if (isLoading)
                return;

            base.OnSelectedValueChanged(e);
        }

        public void LoadVersions(TFServerConnection connection,  string path)
        {
            int v;
            int deletionId;
            string name;
            LizardFileAccess.GetNameAndVersionFromPath(path, out name, out v, out deletionId);
            LoadVersions(connection, name, v, deletionId);
        }

        public void LoadVersions(TFServerConnection connection, string path, int changesetId, int deletionId)
        {
            if (isLoading)
                return;

            int currentVersion = 0;
            int.TryParse(this.Text, out currentVersion);
            bool latest = (this.Text.ToLower() == "latest" || string.IsNullOrEmpty(this.Text));
            if (path == "All")
                return;
            //int changesetId = 0;
            //int deletionId = 0;
            //string name = string.Empty;
            //LizardFileAccess.GetNameAndVersionFromPath(path,out  name, out changesetId, out deletionId);



            try
            {
                isLoading = true;

                VersionControlServer vcServer = connection.VersionControlServer;
                VersionSpec versionSpec;
                if (changesetId == 0)
                    versionSpec = ChangesetVersionSpec.Latest;
                else
                    versionSpec = new ChangesetVersionSpec(changesetId);
                this.Items.Clear();

                bool isItemSelected = false;

                ExtendedItem[] ita = vcServer.GetExtendedItems(new ItemSpec[] { new ItemSpec(path, RecursionType.None) }, DeletedState.Deleted, ItemType.Any)[0];
                List<int> DeletionChangesets = new List<int>();
                foreach (ExtendedItem extItem in ita)
                    DeletionChangesets.Add(extItem.VersionLatest);

                int latestChangesetId = vcServer.GetLatestChangesetId();
                object history = vcServer.QueryHistory(path, versionSpec, deletionId, RecursionType.Full, "",
                    new ChangesetVersionSpec(1), new ChangesetVersionSpec(latestChangesetId), 200, true, false);
                System.Collections.IEnumerable changesets = (System.Collections.IEnumerable)history;

                Changeset first = null;
                foreach (Changeset c in changesets)
                {
                    if (first == null)
                        first = c;
                    if (!DeletionChangesets.Contains(c.ChangesetId))
                    {
                        this.Items.Add(c.ChangesetId);
                        if (currentVersion == c.ChangesetId)
                        {
                            this.SelectedIndex = this.Items.Count - 1;
                            isItemSelected = true;
                        }
                    }
                }
                if (first == null || !DeletionChangesets.Contains(first.ChangesetId))
                {
                    int pi = this.SelectedIndex;
                    this.Items.Insert(0, "Latest");
                    if (this.SelectedIndex == pi)
                    {
                        int nextIdx = pi + 1;
                        this.SelectedIndex = nextIdx;
                    }
                }
                if (!isItemSelected && this.Items.Count > 0)
                    this.Text = changesetId.ToString();
                int pib = this.SelectedIndex;
                this.Items.Insert(1, "Browse...");
                if (this.SelectedIndex > 0 && this.SelectedIndex == pib)
                    this.SelectedIndex++;
                if (latest)
                    this.SelectedIndex = 0;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                isLoading = false;
            }

        }
    }
}
